<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include ('../global/initialize.php');

aos_header('Upgrade details');

$upgrade_id = formatInput($_GET['id']);
if (!is_numeric($upgrade_id)) {
	$upgrade_id = -1;
}

$query = "SELECT * FROM upgrades WHERE upgrade_id = " . $upgrade_id;
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
print "<div class=\"blok\">";
if (isset ($row['name'])) {
	$name = translate($row['name']);
	$description = translate($row['description']);
	$time_cost = $row['time_cost'];
	$time_add = $row['time_add'];
	$gold_cost = $row['gold_cost'];
	$gold_add = $row['gold_add'];
	$turn = getCurrentTurn();

	if ($row['discount_turn'] != -1 && $turn > $row['discount_turn']) {
		$time_cost = round(0.25 * $time_cost);
		$time_add = round(0.25 * $time_add);
		$gold_cost = round(0.25 * $gold_cost);
		$gold_add = round(0.25 * $gold_add);
	}

	print ("<a href=\"upgrade.php\" title=\"" . translate('back to upgrades') . "\" >" . translate('Back to upgrades') . "</a><br /><br />\r\n");

	print ("<strong>$name</strong><br /><br />\r\n");

	$query2 = "SELECT MAX(level) " .
	"FROM upgrade_status " .
	"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND achieved = 1 AND upgrade_id = $upgrade_id";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$current_level = $row2['MAX(level)'] + 1;

	if ($current_level > $row['max_level']) {
		$current_level = translate("Not available");
	}

	print (translate("Gold cost") . ": <strong class=\"gold\">" . number_format($gold_cost + ($current_level -1) * $gold_add) . "</strong><br />\r\n");
	print (translate("Time to upgrade") . ": " . ($time_cost + ($current_level -1) * $time_add) . " days<br />\r\n");
	print (translate("Maximum level") . ": " . $row['max_level'] . "<br />\r\n");
	print (translate("Next level") . ": " . $current_level . "<br />\r\n");

	if ($row['discount_turn'] != -1) {
		$countdown = $row['discount_turn'] - $turn;
		if ($countdown < 1) {
			$countdown = translate("Already available");
		} else
			if ($countdown == 1) {
				$countdown = "1 " . translate("day");
			} else {
				$countdown = $countdown . translate("days");
			}

		print ("<br />" . translate("Time until price reduction") . ": " . $countdown . "<br />\r\n");
		print (translate("Price reduction from") . ": " . $row['discount_date'] . "<br /><br />\r\n");
	}

	if ($row['requires'] != -1) {
		$query2 = "SELECT name, upgrade_id FROM upgrades WHERE upgrade_id = " . $row['requires'];
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		print (translate("Requires upgrade") . ": <a href=\"upgradeinfo.php?id=" . $row2['upgrade_id'] . "\">" . translate($row2['name']) . "</a><br />\r\n");
	} else {
		print (translate("Requires upgrade") . ": " . translate("none") . "<br />\r\n");
	}

	$query2 = "SELECT name, upgrade_id " .
	"FROM upgrades " .
	"WHERE requires = $upgrade_id";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$num = mysql_numrows($result2);
	$i = 0;
	print (translate("Allows upgrade") . ": ");
	if ($num > 0) {
		while ($i < $num) {
			if ($i != 0) {
				print (", ");
			}
			print ("<a href=\"upgradeinfo.php?id=" . mysql_result($result2, $i, "upgrade_id") . "\">" . translate(mysql_result($result2, $i, "name")) . "</a>");
			$i++;
		}
	} else {
		print (" " . translate('none'));
	}
	print ("<br />\r\n");

	$query2 = "SELECT name, building_id " .
	"FROM buildings " .
	"WHERE upgrade = $upgrade_id";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$num = mysql_numrows($result2);
	$i = 0;
	print (translate('Allows construction of building') . ': ');
	if ($num > 0) {
		while ($i < $num) {
			if ($i != 0) {
				print (", ");
			}
			print ("<a href=\"../villages/building.php?id=" . mysql_result($result2, $i, "building_id") . "\">" . translate(mysql_result($result2, $i, "name")) . "</a>");
			$i++;
		}
	} else {
		print (" " . translate("none"));
	}
	print ("<br />\r\n");

	print ("<br /><strong>" . translate("Description") . "</strong><br /><pre>\r\n");
	print (wordwrap($description, 80) . "</pre><br />\r\n");

	$query = "SELECT count(*) FROM upgrade_status WHERE ruler_id = " . $_SESSION['ruler_id'];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$upgrades_set = $row['count(*)'];

	$result2 = mysql_query("SELECT SUM(max_level) FROM upgrades") or die("Query failed: " . mysql_error());
	$row = mysql_fetch_array($result2, MYSQL_ASSOC);
	$total_upgrades_available = $row['SUM(max_level)'];

	$query = "SELECT count(*) FROM upgrade_status WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND achieved = 0";
	$result2 = mysql_query($query) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$total = $row2['count(*)'];

	if ($total_upgrades_available - $upgrades_set > 0 && $total < getQueueLength()) {
		print ("<form action=\"que_upgrade.php\" method=\"post\">\r\n");
		print ("<input type=\"hidden\" name=\"$upgrade_id\" value=\"true\" />\r\n");
		print ("<input type=\"submit\" value=\"" . translate('Queue this upgrade') . "\" />\r\n");
		print ("</form>\r\n");
	}
} else {
	transprint("Invalid upgrade id supplied.");
}
?>
					</div>
<?php
aos_footer();
?>